# DjangoDE, an integrated development environment for Django
# Copyright (C) 2010-2011 Andrew Wilkinson
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

import sys
import unittest

from djangode import global_objects
from djangode.gui import MainWindow
from djangode.gui.python_editor import PythonEditor
from djangode.utils.qt import Qt, QtCore, QtGui

class TestPythonHighlighter(unittest.TestCase):
    def setUp(self):
        if hasattr(sys, "last_type"):
            del sys.last_type

        global_objects.main_window = MainWindow()

    def tearDown(self):
        global_objects.main_window.close()
        global_objects.main_window = None

    def test_comment_override_keyword(self):
        """
        Test that when you type something like '# is in def for' that the key words are coloured grey.
        """
        global_objects.main_window.actions["new_file"].trigger()
        editor = global_objects.main_window.editor.widget(0)
        editor.setFocus(QtCore.Qt.OtherFocusReason)

        for char in "x # is in def for":
            self.send_event(editor, QtGui.QKeyEvent(QtCore.QEvent.KeyPress, ord(char), QtCore.Qt.NoModifier, char))

        self.assert_(not hasattr(sys, "last_type")) # Check that there were no exceptions while processing events.

        block = editor.document().begin()
        layout = block.layout()

        additional_formats = layout.additionalFormats()
        format =  self.get_format(additional_formats, 5)

        foreground = format.foreground()
        colour = foreground.color().rgba()
        self.assert_(hex(colour) == "0xff808080L", hex(colour))

    def test_comment_override_string(self):
        """
        Test that when you type something like '# "abc"' that the string is coloured grey.
        """
        global_objects.main_window.actions["new_file"].trigger()
        editor = global_objects.main_window.editor.widget(0)
        editor.setFocus(QtCore.Qt.OtherFocusReason)

        for char in "x # 'abc'":
            self.send_event(editor, QtGui.QKeyEvent(QtCore.QEvent.KeyPress, ord(char), QtCore.Qt.NoModifier, char))

        self.assert_(not hasattr(sys, "last_type")) # Check that there were no exceptions while processing events.

        block = editor.document().begin()
        layout = block.layout()

        additional_formats = layout.additionalFormats()
        format =  self.get_format(additional_formats, 5)

        foreground = format.foreground()
        colour = foreground.color().rgba()
        self.assert_(hex(colour) == "0xff808080L", hex(colour))

    def test_multiple_strings_single_line(self):
        """
        Test that when you type more than one string on a line they are all highlighted.
        """

        global_objects.main_window.actions["new_file"].trigger()
        editor = global_objects.main_window.editor.widget(0)
        editor.setFocus(QtCore.Qt.OtherFocusReason)

        for char in "'abc' 'def' \"ghi\" \"jkl\"":
            self.send_event(editor, QtGui.QKeyEvent(QtCore.QEvent.KeyPress, ord(char), QtCore.Qt.NoModifier, char))

        self.assert_(not hasattr(sys, "last_type")) # Check that there were no exceptions while processing events.

        block = editor.document().begin()
        layout = block.layout()

        additional_formats = layout.additionalFormats()

        first_format =  self.get_format(additional_formats, 1)
        first_foreground = first_format.foreground()
        first_colour = first_foreground.color().rgba()

        for i in [6, 12, 18]:
            format =  self.get_format(additional_formats, i)
            self.assert_(format is not None, i)
            foreground = format.foreground()
            colour = foreground.color().rgba()
            self.assertEquals(hex(colour), hex(first_colour))

    def send_event(self, editor, event):
        global_objects.app.sendEvent(editor, event)
        global_objects.app.processEvents()

    def get_format(self, format_list, pos):
        text_format = None
        for format_item in format_list:
            if pos >= format_item.start and pos < format_item.start + format_item.length:
                text_format = format_item.format
        return text_format
